#include <stdio.h>

int lcm(int a, int b);
int gcd(int a, int b);
int factor(int a, int b, int c);
int main (int argc, const char * argv[]) {
    int caseN;
	int p, e, i, d;
	int pp=23,pe=28,pi=33;
	int inc, ans;
	for (caseN = 1; ; caseN++) {
		scanf("%d %d %d %d",&p,&e,&i,&d);
		if(p==-1&&e==-1&&i==-1&&d==-1) break;
		p%=pp, e%=pe, i%=pi;
		inc = lcm(lcm(pp,pe),pi);
		for (ans = (factor(pp,pe,pi)*i + factor(pp,pi,pe)*e + factor(pe,pi,pp)*p) % inc; ans<=d; ans += inc);
		printf("Case %d: the next triple peak occurs in %d days.\n", caseN, ans - d);
	}
    return 0;
}

//see china shengyu principle
int factor(int a, int b, int c)
{
	int inc=lcm(a,b);
	int sum;
	for (sum=inc; sum%c!=1; sum+=inc);
	return sum;
}

int lcm(int a, int b)
{
	return a*b/gcd(a, b);
}

int gcd(int a, int b)
{
	int temp;
	if (a==0) {
		return b;
	}
	if (b==0) {
		return a;
	}
	if (a<b) {
		temp = a;
		a=b;
		b=temp;
	}
	while (b!=0) {
		temp = b;
		b=a%b;
		a=temp;
	}
	return a;
}